<?php

class Chat_model extends CI_Model
{
    //头像投稿
    public function uploadAvatar()
    {
        $this->load->database();

        $this->db->trans_start();

        $data = [
            'uid'        => $_POST['uid'],
            'submit_time'=> date('Y-m-d H:i:s',time()),
            'status'     => 1,
        ];

        $basedir = FCPATH . 'public/upload/avatar';

        $res = uploadImageCommon($_FILES['image'],$basedir);

        if ($res['ec'] == 400)
        {
            $this->db->close();
            displayJson(400,$res['em']);
        }

        $res['url'] = str_replace("\\", "/", $res['url']);

        //压缩图片
        $dst_img = $basedir.'/YA_'.time().'_'.rand(10000,99999).'.'.$res['type']; //压缩后名字

        $this->load->library("IMGReduce");

        $IMGReduce = new IMGReduce();

        $IMGReduce->compressImg($res['url'],$dst_img);

        //删除原来的图片
        @unlink($res['url']);

        $real_url   = substr($dst_img,strpos($dst_img,'public'));

        $real_url   = str_replace("\\", "/", $real_url);

        $data['image'] = $real_url;

        $this->db->insert('verify_log', $data);

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            $this->db->close();
            displayJson(400,$this->db->_error_message());
        }

        $this->db->close();

        //sendEmail($this);

        displayJson(200,'投稿成功！');
    }
    
    //发送图片/视频
    public function sendImage()
    {
        $ret = ['ec' => 400];
            
        $this->load->database();

        $this->db->select('A.id,A.nick_name,A.province,A.city,B.url');
        $this->db->from('user_log as A');
        $this->db->join('avatar AS B', 'B.id = A.avatar_id', 'LEFT');
        $this->db->where('A.id',$_POST['uid']);
        $query = $this->db->get();
        $user  = $query->row();

        $this->db->trans_start();

        $data = [
            'uid'        => $_POST['uid'],
            'to_uid'     => $_POST['to_uid'],
            'create_time'=> date('Y-m-d H:i:s',time()),
        ];

        $date    = date('Ymd');
        $basedir = FCPATH . 'public/upload/file/'.$date;

        $res = uploadImageCommon($_FILES['image'],$basedir);

        if ($res['ec'] == 400)
        {
            $this->db->close();
            
            $ret['em'] = $res['em'];
            
            return $ret;
        }

        $res['url'] = str_replace("\\", "/", $res['url']);

        if (!in_array($res['type'],['gif','mp4']))
        {
            //压缩图片
            $dst_img = $basedir.'/YA_c'.time().'_'.rand(10000,99999).'.'.$res['type']; //压缩后名字
    
            $this->load->library("IMGReduce");
    
            $IMGReduce = new IMGReduce();
    
            $IMGReduce->compressImg($res['url'],$dst_img);
    
            //删除原来的图片
            @unlink($res['url']);
    
            $real_url   = substr($dst_img,strpos($dst_img,'public'));
        } else {
            $real_url   = substr($res['url'],strpos($res['url'],'public'));
        }
        
        $real_url   = str_replace("\\", "/", $real_url);

        if ($res['type'] == 'mp4')
        {
             $data['content'] = '<video id="video" src="'.$real_url.'" style="max-width: 100%;border-radius:0px" controls="controls">您的浏览器不支持 video 标签。</video>';
            
        } else {
            $data['content'] = '<img src="'.$real_url.'" class="pic_u" style="max-width: 100%;border-radius:0px"  alt="">';
        }
        
        $this->db->insert('content', $data);

        $this->db->trans_complete();

        if ($this->db->trans_status() === FALSE) {
            $this->db->close();
            
            $ret['em'] = $this->db->_error_message();
            
            return $ret;
        }

        $this->db->close();
        
        $ci   = !empty($user->city) ? $user->city : $user->province;
        $city = str_replace('市', '', $ci);

        $info = [
            'content' => $data['content'],
            'time'    => date('H:i',time()),
            'avatar'  => base_url().$user->url,
            'nick'    => $user->nick_name,
            'city'    => $city,
        ];
        
        $ret['ec']   = 200;
        $ret['em']   = '发送成功！';
        $ret['data'] = $info;
            
        return $ret;
    }

}